package cn.zw.netty.InitMediator;


import cn.zw.netty.Controller.Action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Controller;

import java.lang.reflect.Method;
import java.util.Map;

//初始化中间者模式需要用到的对象
public class InitMediator implements ApplicationListener<ContextRefreshedEvent>,Ordered {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
         ApplicationContext context= event.getApplicationContext();
        Map<String, Object> map=context.getBeansWithAnnotation(Controller.class);
        for (String s : map.keySet()) {
            Object bean=map.get(s);
            Method[] methods= bean.getClass().getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(Action.class)){
                   Action action= method.getAnnotation(Action.class);
                    String actionName= action.value();
                    Mediator.beanMap.put(actionName,new MethodBean(method,bean));
                }
            }
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
